//
//  TimeViewController.m
//  BigNerdRanchGuidesC7-ViewControllers_HypnoTime
//
//  Created by Rafal on 4/7/13.
//  Copyright (c) 2013 Rafal Rebacz. All rights reserved.
//

#import "TimeViewController.h"
#import <QuartzCore/QuartzCore.h>

@implementation TimeViewController

- (id)init
{
    self = [super init];

    if (self)
    {
        UITabBarItem* tbi = [self tabBarItem];
        [tbi setTitle:@"Time"];
    }
        
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor greenColor]];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self showCurrentTime:nil];
}

-  (void)viewDidAppear:(BOOL)animated
{
    [self slideInButton];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"%@ finished: %d", anim, flag);

    if(flag && [[anim valueForKey:@"id"] isEqual:@"slideInAnimation"])
    {
        [self fadeButton];
    }
}

- (void)showCurrentTime:(id)sender
{
    NSDate* now = [NSDate date];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeStyle = NSDateFormatterLongStyle;
    
    _timeLbl.text = [formatter stringFromDate:now];
    
//    [self spinTimeLbl];
    [self bounceTimeLbl];
}

- (void)spinTimeLbl
{
    CABasicAnimation *spinningAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    [spinningAnimation setDelegate:self];
    CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [spinningAnimation setTimingFunction:tf];
    [spinningAnimation setToValue:[NSNumber numberWithFloat:M_PI * 2.0]];
    [spinningAnimation setDuration:1.0f];
    
    [_timeLbl.layer addAnimation:spinningAnimation forKey:@"spinningAnimation"];
}

- (void)bounceTimeLbl
{
    CAKeyframeAnimation *bounce = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    CATransform3D forward1 = CATransform3DMakeScale(1.3, 1.3, 1);
    CATransform3D backward1 = CATransform3DMakeScale(0.7, 0.7, 1);
    CATransform3D forward2 = CATransform3DMakeScale(1.2, 1.2, 1);
    CATransform3D backward2 = CATransform3DMakeScale(0.9, 0.9, 1);
    [bounce setValues:@[[NSValue valueWithCATransform3D:forward1],
                        [NSValue valueWithCATransform3D:backward1],
                        [NSValue valueWithCATransform3D:forward2],
                        [NSValue valueWithCATransform3D:backward2]]];
    [bounce setDuration:0.6];
    
    CAKeyframeAnimation *opacity = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    CGFloat visible = 1.0;
    CGFloat invisible = 0.0;
    [opacity setValues:@[
     [NSNumber numberWithFloat:visible],
     [NSNumber numberWithFloat:invisible],
     [NSNumber numberWithFloat:visible],
     [NSNumber numberWithFloat:invisible]]];
    [opacity setDuration:0.6];
    
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    [animationGroup setAnimations:@[bounce, opacity]];
    [animationGroup setDuration:0.6];
    [_timeLbl.layer addAnimation:animationGroup forKey:nil];
}

- (void)slideInButton
{
    CABasicAnimation *slideInAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    [slideInAnimation setDelegate:self];
    CAMediaTimingFunction *tf = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [slideInAnimation setTimingFunction:tf];
    [slideInAnimation setFromValue:[NSValue valueWithCGPoint:CGPointMake(-_button.frame.size.width, _button.center.y)]];
    [slideInAnimation setToValue:[NSValue valueWithCGPoint:CGPointMake(_button.center.x, _button.center.y)]];
    [slideInAnimation setValue:@"slideInAnimation" forKey:@"id"];
    
    [_button.layer addAnimation:slideInAnimation forKey:@"slideInButtonAnimation"];
}

- (void)fadeButton
{
    CABasicAnimation *fadeAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [fadeAnimation setFromValue:[NSNumber numberWithFloat:1.0]];
    [fadeAnimation setToValue:[NSNumber numberWithFloat:0.0]];
    [fadeAnimation setValue:@"fadeAnimation" forKey:@"id"];
    [fadeAnimation setAutoreverses:YES];
//    [fadeAnimation setRemovedOnCompletion:NO];
    [fadeAnimation setRepeatCount:HUGE_VALF];
    [_button.layer addAnimation:fadeAnimation forKey:@"slideInButtonAnimation"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    _timeLbl = nil;
}

@end
